package com.telenav.util;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;

import com.telenav.app.R;

public class SmileysUtil {

	public static final int[] smilesIds = { R.drawable.yy_emotion_icon2,
			R.drawable.yy_emotion_icon3, R.drawable.yy_emotion_icon11,
			R.drawable.yy_emotion_icon4, R.drawable.yy_emotion_icon12,
			R.drawable.yy_emotion_icon5, R.drawable.yy_emotion_icon13,
			R.drawable.yy_emotion_icon6, R.drawable.yy_emotion_icon14,
			R.drawable.yy_emotion_icon7, R.drawable.yy_emotion_icon15,
			R.drawable.yy_emotion_icon8, R.drawable.yy_emotion_icon16,
			R.drawable.yy_emotion_icon9, R.drawable.yy_emotion_icon17,
			R.drawable.yy_emotion_icon10, R.drawable.yy_emotion_icon18 };

	public static final String[] mSmileyTexts = { ":-)", ":-(", ";-)", ":-P",
			"=-O", ":-*", ":O", "B-)", ":-$", ":-!", ":-[", "O:-)", ":-\\",
			":\'", ":-X", ":-D", "o_O" };
	
	
    private final Context mContext;
    
    private final Pattern mPattern;
    
    private final HashMap<String, Integer> mSmileyToRes;

	private static SmileysUtil sInstance = null;

	public static SmileysUtil getInstance() {
		return sInstance;
	}

	public static void init(Context context) {
		if (sInstance == null)
			sInstance = new SmileysUtil(context);
	}

    private SmileysUtil(Context context) {
        mContext = context;
        mSmileyToRes = buildSmileyToRes();
        mPattern = buildPattern();
    }

	public static void destroyInstance() {
		if (sInstance != null) {
			sInstance = null;
		}
	}
	
    private HashMap<String, Integer> buildSmileyToRes() {
    	HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(mSmileyTexts.length);
        for (int i = 0; i < mSmileyTexts.length; i++) {
            smileyToRes.put(mSmileyTexts[i], smilesIds[i]);
        }
        return smileyToRes;
    }

	public int getSmileyID(int position) {
		return smilesIds[position];
	}

	public String getSmileyText(int position) {
		return mSmileyTexts[position];
	}

	public int getSmileCount() {
		return smilesIds.length;
	}

	private Pattern buildPattern() {
		// Set the StringBuilder capacity with the assumption that the average
		// smiley is 3 characters long.
		StringBuilder patternString = new StringBuilder(mSmileyTexts.length * 3);
		patternString.append('(');
		for (String s : mSmileyTexts) {
			patternString.append(Pattern.quote(s));
			patternString.append('|');
		}
		// Replace the extra '|' with a ')'
		patternString.replace(patternString.length() - 1,
				patternString.length(), ")");

		return Pattern.compile(patternString.toString());
	}

	public SpannableString getSmileSpan(String text) {
		SpannableString spanStr = new SpannableString(text);
		Matcher matcher = mPattern.matcher(text);
		while (matcher.find()) {
			Drawable d = mContext.getResources().getDrawable(mSmileyToRes.get(matcher.group()));
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
			spanStr.setSpan(span, matcher.start(), matcher.end(),
					Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		}
		return spanStr;
	}
}
